package de.jmda.gen.java.naming;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import junit.framework.Assert;

import org.junit.Test;

public class JUT_ImportManager
{
	@Test
	public void testGenerateImportStatementsEmptyForNoUseCalls()
	{
		ImportManager importManager =
				new ImportManager(this.getClass().getPackage().getName());
		
		assertEquals("", importManager.generateImportStatements().toString());
	}

	@Test
	public void testGenerateImportStatementsEmptyForUnnecessaryImports()
	{
		ImportManager importManager =
				new ImportManager(this.getClass().getPackage().getName());
		
		importManager.useType(String.class.getName());    // no import for java.lang
		importManager.useType(this.getClass().getName()); // no import for same package

		assertEquals("", importManager.generateImportStatements().toString());
	}

	@Test
	public void testGenerateImportStatements()
	{
		String packageName = this.getClass().getPackage().getName();
		
		ImportManager importManager = new ImportManager(packageName);
		
		String qualifiedTypeName = packageName + ".test.Test";
		
		importManager.useType(qualifiedTypeName + "1");
		importManager.useType(qualifiedTypeName + "2");
		
		assertTrue(
				importManager.generateImportStatements()
				             .indexOf(qualifiedTypeName + "1") > -1);
		
		assertTrue(
				importManager.generateImportStatements()
				             .indexOf(qualifiedTypeName + "2") > -1);
	}

	@Test
	public void testUseType()
	{
		String packageName = this.getClass().getPackage().getName();
		
		ImportManager importManager = new ImportManager(packageName);
		
		String qualifiedTypeName = this.getClass().getName();

		assertEquals(
				this.getClass().getSimpleName(),
				importManager.useType(qualifiedTypeName).toString());
	}
	
	@Test
	public void testAddStaticImport()
	{
		String staticImport = Assert.class.getName() + ".assertTrue";
		
		ImportManager importManager = new ImportManager("some.package");
		
		importManager.addStaticImport(staticImport);

		assertTrue(
				importManager.generateImportStatements()
				             .indexOf(staticImport) > -1);
	}

	@Test
	public void testAddStaticImportWithWildcardImportAlreadyExisting()
	{
		String staticImport = Assert.class.getName() + ".assertTrue";
		
		ImportManager importManager = new ImportManager("some.package");
		
		importManager.addStaticImport(Assert.class.getName() + ".*");
		importManager.addStaticImport(staticImport);

		assertTrue(
				importManager.generateImportStatements()
				             .indexOf(staticImport) < 0);
		assertTrue(
				importManager.generateImportStatements()
				             .indexOf(Assert.class.getName() + ".*") > 0);
	}
	
	@Test
	public void testSandbox()
	{
		ImportManager importManager =
				ImportManagerProvider.demandImportManager("some.target.package");
		
		String type     = importManager.useType("some.target.package.Type");
		String utilDate = importManager.useType(java.util.Date.class);
		String sqlDate  = importManager.useType(java.sql.Date.class);
		String string   = importManager.useType(String.class);

		System.out.println(
				"import statements:\n" + importManager.generateImportStatements());

		System.out.println("type     [" + type     + "]");
		System.out.println("utilDate [" + utilDate + "]");
		System.out.println("sqlDate  [" + sqlDate  + "]");
		System.out.println("string   [" + string   + "]");
	}
}